from sqlalchemy import Column, String, DateTime
from sqlalchemy.orm import sessionmaker

from init.mysql import Base, engine


class StockTake(Base):
    __tablename__ = 'stock_take'
    dm = Column(String(6), primary_key=True)
    mc = Column(String(255))
    jys = Column(String(2))

    def __init__(self, dm, mc, jys):
        self.dm = dm
        self.mc = mc
        self.jys = jys
        self.update_time = Column(DateTime)

    @staticmethod
    def not_exist_insert(data_list):
        session = sessionmaker(bind=engine)()
        for data in data_list:
            existing_stock_second_new = session.query(StockTake).filter_by(dm=data['dm']).first()
            if existing_stock_second_new:
                continue
            else:
                session.add(StockTake(data['dm'], data['mc'], data['jys']))
        session.commit()
        session.close()


Base.metadata.create_all(engine)
